package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.dao.PaasPrintTemplateDao;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.domain.entity.PaasPrintTemplateEntity;
import com.xbongbong.paas.model.PaasAppModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.script.pojo.dto.ProductDTO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ReceiptOrderEnum;
import com.xbongbong.saas.service.RepairReceiptOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author WuHB
 * @date 2023/02/10 14:49
 */
@Service("repairReceiptOrderService")
public class RepairReceiptOrderServiceImpl implements RepairReceiptOrderService {

    @Resource
    private PaasPrintTemplateDao paasPrintTemplateDao;

    @Resource
    private PaasAppModel paasAppModel;

    /**
     * 修复回执单打印模板
     *
     * @param productDTO
     * @return
     * @throws XbbException
     */
    @Override
    public String repairReceiptOrderPrintExplain(ProductDTO productDTO) throws XbbException {
        List<String> corpidList = productDTO.getCorpidList();
        if (CollectionsUtil.isEmpty(corpidList)) {
            Map<String, Object> param = new HashMap<>();
            param.put("columns", "corpid");
            param.put("del", 0);
            param.put("enable", 1);
            param.put("alias", XbbRefTypeEnum.WORK_ORDER_V2_CENTER.getAlias());
            List<PaasAppEntity> entitys = paasAppModel.findEntitys(param);
            if (CollectionsUtil.isEmpty(entitys)) {
                return "未找到开通新工单的公司";
            }
            corpidList = entitys.stream().map(PaasAppEntity::getCorpid).collect(Collectors.toList());
        }
        corpidList.add("0");
        Map<String, Object> param = new HashMap<>();
        param.put("del", 0);
        param.put("businessType", XbbRefTypeEnum.RECEIPT_ORDER.getCode());
        for (String corpid : corpidList) {
            param.put("corpid", corpid);
            List<PaasPrintTemplateEntity> printTemplateEntityList = paasPrintTemplateDao.findEntitys(param);
            if (CollectionsUtil.isEmpty(printTemplateEntityList)) {
                continue;
            }

            for (PaasPrintTemplateEntity paasPrintTemplateEntity : printTemplateEntityList) {
                String html = paasPrintTemplateEntity.getHtml();
                if (StringUtil.isEmpty(html)) {
                    continue;
                }
                JSONArray jsonArray = JsonHelperUtil.parseArray(html);
                String dispalyAttrList = "[{\"attr\":\"text_1\",\"attrName\":\"服务项目名称\",\"saasAttr\":\"text_1\",\"isShow\":true},{\"attr\":\"serialNo\",\"attrName\":\"服务项目编号\",\"saasAttr\":\"serialNo\",\"isShow\":true},{\"attr\":\"text_2\",\"attrName\":\"单位\",\"saasAttr\":\"text_2\",\"isShow\":true},{\"attr\":\"num_1\",\"attrName\":\"单价\",\"saasAttr\":\"num_1\",\"isShow\":true},{\"attr\":\"num_12\",\"attrName\":\"折扣(%)\",\"saasAttr\":\"num_12\",\"isShow\":true},{\"attr\":\"num_13\",\"attrName\":\"售价(元)\",\"saasAttr\":\"num_13\",\"isShow\":true},{\"attr\":\"num_14\",\"attrName\":\"数量\",\"saasAttr\":\"num_14\",\"isShow\":true},{\"attr\":\"num_15\",\"attrName\":\"售价小计\",\"saasAttr\":\"num_15\",\"isShow\":true}]";
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    if (Objects.equals(ReceiptOrderEnum.SERVICE_PROJECT.getAttr(), jsonObject.getString("type"))) {
                        JSONObject options = jsonObject.getJSONObject("options");
                        options.put("dispalyAttrList", JsonHelperUtil.parseArray(dispalyAttrList));
                    }
                }
                paasPrintTemplateEntity.setHtml(JSONObject.toJSONString(jsonArray));
            }
            paasPrintTemplateDao.updateBatchAll(printTemplateEntityList);
        }

        return null;
    }
}
